package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.information.domain.back.RecomBack;
import com.ruoyi.information.mapper.back.RecomBackMapper;
import com.ruoyi.information.mapper.back.UserReserveParkBackMapper;
import com.ruoyi.information.service.back.IRecomBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 推荐列表Controller
 *
 * @author ruoyi
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/recomBack")
public class RecomBackController extends BaseController {
    @Autowired
    private IRecomBackService recomService;

    @Autowired
    private UserReserveParkBackMapper userReserveParkMapper;

    @Autowired
    private RecomBackMapper recomMapper;

    /**
     * 查询推荐列表列表
     */
    @RequiresPermissions("information:recom:list")
    @GetMapping("/list")
    public TableDataInfo list(RecomBack recom) {
        startPage();
        List<RecomBack> list = recomService.selectRecomList(recom);
        return getDataTable(list);
    }

    /**
     * 空间类型
     */
    @GetMapping("/space")
    public AjaxResult space() {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "凑字数 水");
        /*园区*/
        ajaxResult.put("park", userReserveParkMapper.selectParkList());
        /*楼宇商用*/
        ajaxResult.put("buildingCommercial", userReserveParkMapper.selectBuildingCommercialList());
        /*楼宇办公*/
        ajaxResult.put("buildingOffice", userReserveParkMapper.selectBuildingOfficeList());
        /*仓库*/
        ajaxResult.put("warehouse", userReserveParkMapper.selectWarehouseList());
        /*厂房*/
        ajaxResult.put("workShop", userReserveParkMapper.selectWorkShopList());
        /*公寓*/
        ajaxResult.put("apartment", userReserveParkMapper.selectApartmentList());
        /*拍卖*/
        ajaxResult.put("auction", userReserveParkMapper.selectAuctionList());
        /*土地*/
        ajaxResult.put("land", userReserveParkMapper.selectLandList());
        /*服务案例*/
        ajaxResult.put("wenzhang", recomMapper.selectWenzhang());
        /*资讯*/
        ajaxResult.put("article", recomMapper.selectArticle());
        /*企业需求*/
        ajaxResult.put("companyNeed", recomMapper.selectCompanyNeed());
        /*云课堂*/
        ajaxResult.put("classroom", recomMapper.selectClassroom());
        /*服务资讯*/
        ajaxResult.put("serviceArticle", recomMapper.selectServiceArticle());
        /*区域营商环境介绍*/
        ajaxResult.put("intro", recomMapper.selectIntro());
        return ajaxResult;
    }

    /**
     * 获取推荐列表详细信息
     */
    @RequiresPermissions("information:recom:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(recomService.selectRecomById(id));
    }

    /**
     * 新增推荐列表
     */
    @RequiresPermissions("information:recom:add")
    @Log(title = "推荐列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecomBack recom) {
        RecomBack recom1 = new RecomBack();
        recom1.setRecomId(recom.getRecomId());
        recom1.setType(recom.getType());
        recom1.setPosition(recom.getPosition());
        List<RecomBack> recoms = recomMapper.selectRecomList(recom1);
        if (recoms != null && recoms.size() > 0) {
            return error("不可重复");
        }

        return toAjax(recomService.insertRecom(recom));
    }

    /**
     * 修改推荐列表
     */
    @RequiresPermissions("information:recom:edit")
    @Log(title = "推荐列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecomBack recom) {

        RecomBack byId = recomMapper.selectRecomById(recom.getId());
        if (byId.getPosition().equals(recom.getPosition()) &&
                byId.getType().equals(recom.getType()) &&
                byId.getRecomId().equals(recom.getRecomId())) {
            return success();
        }

        RecomBack recom1 = new RecomBack();
        recom1.setRecomId(recom.getRecomId());
        recom1.setType(recom.getType());
        recom1.setPosition(recom.getPosition());
        List<RecomBack> recoms = recomMapper.selectRecomList(recom1);
        if (recoms != null && recoms.size() > 0) {
            return error("不可重复");
        }

        return toAjax(recomService.updateRecom(recom));
    }

    /**
     * 删除推荐列表
     */
    @RequiresPermissions("information:recom:remove")
    @Log(title = "推荐列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(recomService.deleteRecomByIds(ids));
    }
}
